<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\OverSeasBa;

use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\RemovaleFnSkuTranLog;
use Illuminate\Support\Facades\DB;
class RemovaleFnSkuAjax {

    /**
     * 换标
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function fnSkuInTrans($param){
        try{
            $detail = $param['detail'];
            $exist = [];
            $_detail = [];
            foreach ($detail as $item){
                $id = $item['id'];
                if(array_key_exists($id,$exist)){
                    $exist[$id] += $item['transfer_num'];
                }else{
                    $exist[$id] = $item['transfer_num'];
                }
                $__detail['account_id'] = $item['account_id'];
                $__detail['fnsku'] = $item['fnsku'];
                $__detail['transfer_num'] = $item['transfer_num'];
                $_detail[$id]['detail'][] = $__detail;
                $_detail[$id]['total'] = $exist[$id];
            }
            $msg = [];
            foreach($_detail as $id => $item){
                $temp = $item['detail'];
                $total = $item['total'];
                DB::beginTransaction();
                $oci = RemovaleInventoryFNSku::query()->lockForUpdate()->find($id);
                if(!$oci){
                    DB::rollBack();
                    continue;
                }
                if($oci->instant < $total){
                    $msg[] = $oci->fnsku;
                    DB::rollBack();
                    continue;
                }
                $count = 0;
                foreach($temp as $_temp){
                    $accountId = $_temp['account_id'];
                    $fnsku = $_temp['fnsku'];
                    $transferNum = $_temp['transfer_num'];
                    $ici = RemovaleInventoryFNSku::query()
                        ->firstOrNew([
                            'warehouse_id'=>$oci->warehouse_id,
                            'account_id'=>$accountId,
                            'fnsku'=>$fnsku,
                        ],['psku_id' => $oci->psku_id]);
                    $ici->instant += $transferNum;
                    $a_flag = $ici->save();
                    if($a_flag){
                        $count++;
                        RemovaleFnSkuTranLog::logs($oci->warehouse_id,['o' => $oci->account_id,'i' => $accountId],['o' => $oci->fnsku,'i' => $fnsku],$transferNum);
                    }
                }
                $oci->instant -= $total;
                $o_flag = $oci->save();
                if(count($temp) == $count && $o_flag){
                    DB::commit();
                }else{
                    DB::rollBack();
                }
            }
            return response()->json([
                'status' => !empty($msg) ? false : true,
                'message' => !empty($msg) ? implode(PHP_EOL,$msg).'数量超出' : '操作成功！'
            ]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }
}
